package class_wuziqi;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.plaf.nimbus.NimbusLookAndFeel;
import javax.swing.table.DefaultTableModel;

public class MainPro extends JFrame{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static final int WIDTH=990;
	private static final int HEIGHT=700;
	public static final int GAMESTART=0X1000;
	public static final int AGREE_START=0X1001;
	public static final int DISAGREE_START=0x1002;
	public static final int DEFEAT=0x2000;
	public static final int AGREE_DEFEAT=0x2001;
	public static final int DISAGREE_DEFEAT=0x2002;
	public static final int REGRET=0X3000;
	public static final int AGREE_REGRET=0X3001;
	public static final int DISAGREE_REGRET=0X3002;
	public static final int DRAW=0X4000;
	public static final int AGREE_DRAW=0X4001;
	public static final int DISAGREE_DRAW=0X4002;
	public static final int REPLAY=0x5000;
	public static final int isWin=0x6000;//
	public static final int isLose=0x60001;//
	private JTextArea user_info;
	private DefaultTableModel dtm;
	private JTable jtable;
	public JTextArea jta;
	private JTextField jtf;
	private JButton send;
	public ServerSocket server;
	private JPanel lp;
	private static MainPro mainframe;
	public Map<Integer, Integer> map =new HashMap<>();
	public boolean isLinked=false;
	public boolean isStart=false;
	public boolean isOver=false;
	public Color chessColor;
	public boolean isMy=false;
	public boolean Win=false;
	
	private MainPro(){
		mainframe=this;
		Jiemian();
		Login login=new Login();
		setGlassPane(login);
		login.setVisible(true);
		try {
			server=new ServerSocket(2828);
			map.put(2828, 2727);
		} catch (IOException e) {
			try {
				server=new ServerSocket(2727);
				map.put(2727, 2828);
			} catch (IOException e1) {
				System.out.println("一台电脑至多运行两个实例！");
			}		
		}
		ServerThread st=new ServerThread(server);
		new Thread(st).start();
	}
	public static MainPro getInstance(){
		return mainframe;
	}
	
	public void sendMessage(Socket socket,Object message) throws IOException{
		ObjectOutputStream oos=new ObjectOutputStream(socket.getOutputStream());
		oos.writeObject(message);
		oos.flush();		
	}

	public void setUser(User user,boolean isSelf){
		if(isSelf){
			user_info.append("昵称："+user.getName()+"\n"+"I P："+user.getIp());
		}
		Object[] row={user.getName(),user.getIp(),user.getLogin_time()};
		System.out.println(user.getName()+" "+user.getIp()+" "+user.getLogin_time());
		dtm.addRow(row);
	}
	private void Jiemian(){
		setTitle("五子棋_局域网");
		setSize(WIDTH, HEIGHT);
		setVisible(true);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
		setLocation((screenSize.width-WIDTH)/2, (screenSize.height-HEIGHT)/2);
		JPanel right=new JPanel();
		right.setLayout(new BoxLayout(right, BoxLayout.PAGE_AXIS));
		right.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
		add(right, BorderLayout.EAST);
		//用户头像与信息
		JPanel right_top=new JPanel();
		right_top.setLayout(new BorderLayout());
		JLabel tx=new JLabel();
		tx.setIcon(new ImageIcon(getClass().getResource("/pic/left.jpeg")));
		right_top.add(tx, BorderLayout.WEST);
		user_info=new JTextArea(3,10);
		right_top.add(user_info, BorderLayout.CENTER);
		right.add(right_top);
		//联机信息显示
		JPanel clientjp=new JPanel();
		clientjp.setLayout(new BorderLayout());
		right.add(clientjp);
		dtm =new DefaultTableModel(new Object[][]{}, new String[]{"昵称","IP","联机时间"});
		jtable=new JTable();
		jtable.setModel(dtm);
		jtable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		clientjp.add(jtable);
		JScrollPane csp=new JScrollPane();
		csp.setPreferredSize(new Dimension(223, 140));
		csp.setViewportView(jtable);
		clientjp.add(csp, BorderLayout.CENTER);
		//聊天窗口
		JPanel chatjp=new JPanel();
		chatjp.setLayout(new BorderLayout());
		right.add(chatjp);
		jta=new JTextArea();
		jta.setEditable(false);
		jta.setPreferredSize(new Dimension(100,250));
		chatjp.add(jta, BorderLayout.CENTER);
		JPanel down=new JPanel();
		down.setLayout(new BorderLayout());
		jtf=new JTextField();
		down.add(jtf, BorderLayout.CENTER);
		send=new JButton("发送");send.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				String o=jtf.getText();
				try {
					sendMessage(Login.getInstance().getSocket(), o);
					jta.append(Login.getInstance().username_text.getText()+">>"+o+"\n");
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		down.add(send, BorderLayout.EAST);
		chatjp.add(down, BorderLayout.SOUTH);		
		//左窗口部分
		lp=new Left_Panel();
		add(lp, BorderLayout.CENTER);
	}

	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			
			@Override
			public void run() {
				try { 
					UIManager.setLookAndFeel(new NimbusLookAndFeel());//定义风格
					new MainPro();
				} catch (UnsupportedLookAndFeelException e) {
					e.printStackTrace();
				}
			}
		});
	}
}
